package com.suncaper.tddmall.controller;

import com.suncaper.tddmall.common.model.Cart;
import com.suncaper.tddmall.common.model.Goods;
import com.suncaper.tddmall.common.model.User;
import com.suncaper.tddmall.service.CartService;
import com.suncaper.tddmall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    CartService cartService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping("")
    public String index(Model model){
        List<Goods> goodsList = goodsService.selectAll();
        model.addAttribute("goodsList",goodsList);
        return "index";
    }

    @GetMapping("/toindex")
    public String toIndex(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
//            System.out.println("登录成功");

        List<Cart> carts = cartService.selectAll(user.getUserId());
        for (Cart cart : carts) {
            Goods goods = goodsService.selectByPrimaryKey(cart.getGoodsId());
            cart.setGoods(goods);
        }
        model.addAttribute("cartList", carts);

        return "index";
    }
}
